/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shopping.front;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import shopping.model.Product;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author mazhao
 */
public class IndexAction extends FrontAction {

    private Log log = LogFactory.getLog(this.getClass());

    // ===========================================
    // index section
    // ===========================================

    private Map<String, List<Product>> indexProducts ;

    public Map<String, List<Product>> getIndexProducts() {
        return indexProducts;
    }

    public void setIndexProducts(Map<String, List<Product>> indexProducts) {
        this.indexProducts = indexProducts;
    }

    @Action(value = "/front/index",
            results = {
                    @Result(name = "success", location = "/WEB-INF/content/front/index.jsp")
            })
    public String index() {
        if (log.isDebugEnabled()) {
            log.debug("enter front/index action");
            log.debug("catalogId:" + catalogId);
        }

        // TODO: !!A LOT OF PROCESS HERE!!!

        // 1) get all catalog
        // 2) get all catalog's full name
        // 3) get 5 product of eache catalog
        // 4) put them in the map 
        this.indexProducts = Product.getLimitProductForEachCatalog();

        if(log.isDebugEnabled()) {
            log.debug("========================= BEGIN ==================================");
            Iterator<Map.Entry<String, List<Product>>> it = this.indexProducts.entrySet().iterator();
            while(it.hasNext()) {
                Map.Entry<String, List<Product>> ent = it.next();
                String fullName = ent.getKey();
                List<Product> prods = ent.getValue();
                log.debug("catalog name:" + fullName);
                for(Product p :prods) {
                    log.debug(p);
                }
            }
            log.debug("======================== END ===================================");
        }

        return SUCCESS;
    }

    // ===========================================
    // catalog section
    // ===========================================
    private int catalogId = 0;

    public int getCatalogId() {
        return catalogId;
    }

    public void setCatalogId(int catalogId) {
        this.catalogId = catalogId;
    }

    /**
     * list all product for the specified catalog
     *
     * @return all products
     */
    public List<Product> getProducts() {
        List<Product> products = Product.getAllProductsOfCatalog(catalogId);

        if (log.isDebugEnabled()) {
            for (Product p : products) {
                log.debug(p);
            }
        }
        return products;
    }

    @Action(value = "/front/catalog",
            results = {
                    @Result(name = "success", location = "/WEB-INF/content/front/catalog.jsp")
            })
    public String catalog() {
        if (log.isDebugEnabled()) {
            log.debug("enter front/catalog action");
            log.debug("catalogId:" + catalogId);
        }

        return SUCCESS;
    }

    // ===========================================
    // detail section
    // ===========================================
    private int productId = 0;
    private Product detail = new Product();

    public Product getDetail() {
        return detail;
    }

    public void setDetail(Product detail) {
        this.detail = detail;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }


    /**
     * show product details.
     *
     * @return product details page
     */
    @Action(value = "/front/detail",
            results = {
                    @Result(name = "success", location = "/WEB-INF/content/front/detail.jsp")
            })
    public String detail() {
        if (log.isDebugEnabled()) {
            log.debug("enter front/detail action");
            log.debug("productId:" + productId);
        }

        detail.setId(productId);
        detail.load();

        return SUCCESS;
    }


}
